﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessWarehouse
// 文件功能描述：仓库管理数据访问层
//
// 
// 创建标识：2012-02-08 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessWarehouse
    {
        /// <summary>
        /// 获取仓库信息
        /// </summary>
        /// <returns></returns>
        public DataSet SelWarehouseInfo(int pagesize, int startindex, string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY ID ASC) rownumber,* " +
                               "from Con_Warehouse) B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString() + strquery;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 仓库数量
        /// </summary>
        /// <returns></returns>
        public int GetWarehouseCount()
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Con_Warehouse";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 保存仓库(新增界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveWarehouse(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "insert into Con_Warehouse(Warehouse_Name,Manager,Remark) Values('" +
                supp[0] + "','" + supp[1] + "','" + supp[2] + "')";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 保存仓库(修改界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveWarehouseEdit(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "update Con_Warehouse set Warehouse_Name='" + supp[1] + "',Manager='" + supp[2] + "',Remark='" +
                supp[3] + "' where ID=" + supp[0];
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteWarehouse(string id)
        {
            //sqldb db = new sqldb();
            //string query = "Delete from Con_Warehouse where ID = '" + id + "'";
            //int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            //return rowcount;
            sqldb db = new sqldb();
            int rowcount = 0;
            string query = "select Flag from Con_Warehouse where ID in(" + id + ")";
            DataSet ds = db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["Flag"].ToString() == "1")
                {
                    return rowcount = -1;
                }
            }
            /*
             * 检查出入库表中是否有要删除的这些仓库的记录如果有则返回0，也就是不能删除了，否则可以删除。
             */
            query = "select count(*) from Con_Instock_Detailed where Warehouse_ID in(" + id + ")";
            rowcount = Convert.ToInt32(db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query));
            query = "select count(*) from Con_Outstock_Detailed where Warehouse_ID in(" + id + ")";
            rowcount += Convert.ToInt32(db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query));
            if (rowcount > 0)
            {
                rowcount = 0;
            }
            else
            {
                query = "Delete from Con_Warehouse where ID in(" + id + ")";
                rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            }
            return rowcount;
        }

        /// <summary>
        /// 根据编号获取一条仓库信息
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public DataSet GetWarehouseInfoOne(string id)
        {
            sqldb db = new sqldb();
            string query = "select * from Con_Warehouse where ID = '" + id + "'";
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        public static string GetWarehouseName(int id)
        {
            sqldb db = new sqldb();
            string query = "select isnull((select Warehouse_Name from Con_Warehouse where ID = " + id.ToString() + "),'') Warehouse_Name";
            object wname = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return wname.ToString();
        }
    }
}
